package com.example.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.score.entity.Admin;
import com.example.score.entity.AdminRole;
import com.example.score.service.AdminRoleService;
import com.example.score.service.AdminService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shenrui
 * @date 2021/9/26
 * @description super对普通管理员的增删改查的管理
 */
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    AdminRoleService adminRoleService;
    /**
     *@description 查询普通管理员列表
     * @return String
     */
    @Secured("ROLE_super")
    @GetMapping("/super_admin_list")
    public String studentManage(HttpSession session) {
        List<AdminRole> adminRoles = adminRoleService.list(new LambdaQueryWrapper<AdminRole>().eq(AdminRole::getRoleId,1));
        List<Long> adminIds = new ArrayList<>();
        for (AdminRole userRole : adminRoles) {
            adminIds.add(userRole.getAdminId());
        }
        List<Admin> adminList = adminService.list(new LambdaQueryWrapper<Admin>().in(Admin::getId,adminIds));
        session.setAttribute("adminList", adminList);
        return "super_admin_list";
    }
    /**
     *@description 改变普通管理员的状态
     * @return String
     */
    @Secured("ROLE_super")
    @RequestMapping("/admin_status/{id}")
    public String adminStatus(@PathVariable("id") Long id,
                                HttpSession session) {
        Admin admin = adminService.getById(id);
        if(admin.getAdminStatus() == 1) {
            admin.setAdminStatus(0);
        } else {
            admin.setAdminStatus(1);
        }
        adminService.updateById(admin);
        return "redirect:/super_admin_list";
    }

    /**
     *@description 添加普通管理员
     * @return String
     */
    @Secured("ROLE_super")
    @PostMapping("/submit_admin_add")
    public String adminAdd(Admin admin, HttpSession session, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(admin.getAdminName()) ) {
            model.addAttribute("adminMsg", "信息不能为空");
            return "super_admin_add";

        }
        //账号不能重复
        if (adminService.count(new LambdaQueryWrapper<Admin>().eq(Admin::getAdminName, admin.getAdminName())) > 0) {
            model.addAttribute("adminMsg", "账号重复");
            return "super_admin_add";
        }
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(admin.getAdminPassword());
        admin.setAdminPassword(hashPass);
        //判断是否成功插入数据库
        if(adminService.save(admin)) {
            return "redirect:/super_admin_list";
        } else {
            model.addAttribute("adminMsg", "添加失败");
            return "super_admin_add";
        }
    }

    /**
     *@description 跳转到修改密码界面
     * @return String
     */
    @Secured("ROLE_super")
    @GetMapping("/admin_password_jump/{id}")
    public String adminPasswordJump(@PathVariable("id") Long id,
                                      HttpSession session) {
        Admin admin = adminService.getById(id);
        session.setAttribute("adminInfo",admin);
        return "redirect:/super_admin_password";
    }

    /**
     *@description 修改管理员密码
     * @return String
     */
    @Secured("ROLE_super")
    @PostMapping("/submit_admin_password")
    public String reviceAdminPassword(Admin admin, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(admin.getAdminPassword())) {
            model.addAttribute("adminMsg", "密码不能为空");
            return "super_admin_password";

        }
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(admin.getAdminPassword());
        admin.setAdminPassword(hashPass);
        //进行更新
        Boolean bool = adminService.updateById(admin);
        if (bool) {
            return "redirect:/super_admin_list";
        } else {
            model.addAttribute("adminMsg", "修改失败");
            return "super_admin_password";
        }

    }


}
